﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FwProject.Comm
{
    public class TimeHelp
    {
        private static readonly object _obj = new object();
        public static int DateTimeToStamp(System.DateTime time)
        {
            System.DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        /// <summary>
        /// 24小时候执行
        /// </summary>
        /// <returns></returns>
        public static TimeSpan DateTime24ToStamp(){

             int hour = 24 - DateTime.Now.Hour;
             if (hour < 16)
                 hour = 8;

            return TimeSpan.FromHours(hour);
        }

        /// <summary>
        /// 根据用户生成系统唯一订单编号
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string OrderNumber(long userId,int payToal) {

            string number = string.Empty;
            lock (_obj) {
                var userLength = userId.ToString().Length;
                var userNumber = userId.ToString().Length > 4 ? userId.ToString().Substring(userLength - 4, 4) : userId.ToString();
                //25位
                number = $"{DateTime.Now.ToString("yyyyMMddhhmmss")}{userNumber}{payToal}".PadRight(25,'0');
            }
            return number;
        }

        public static string GetTimestamp()
        {

            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
    }
}
